package com.lazy.book.service.system.impl;

import com.lazy.book.pojo.system.pojo.dto.common.SelectorQueryDTO;
import com.lazy.book.pojo.system.pojo.po.SysFile;
import com.lazy.book.pojo.system.pojo.vo.common.SelectorVO;
import com.lazy.book.pojo.system.pojo.vo.systempfile.SysTempFileInfoVO;
import com.lazy.book.service.system.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;



@Service
@RequiredArgsConstructor
public class CommonServiceImpl implements CommonService {

    private final ResourceLoader resourceLoader;

    private final SysTempFileService sysTempFileService;

    private final SysFileService sysFileService;

    private final OssClient ossClient;

    private final SysUserService sysUserService;

    private final SysDeptService sysDeptService;

    private final SysRoleService sysRoleService;

    @Override
    public java.util.List<SelectorVO> deptSelector(SelectorQueryDTO dto) {
        return sysDeptService.listSelector(dto);
    }

    @Override
    public java.util.List<SelectorVO> userSelector(SelectorQueryDTO dto) {
        return sysUserService.listSelector(dto);
    }

    @Override
    public java.util.List<SelectorVO> roleSelector(SelectorQueryDTO dto) {
        return sysRoleService.listSelector(dto);
    }

    @Override
    public java.util.List<SelectorVO> postSelector(SelectorQueryDTO dto) {
        return null;
    }

    @Override
    public java.util.List<SelectorVO> dictSelector(String typeCode) {
        return null;
    }

    @Override
    public java.util.List<SelectorVO> dictSelectorByTypeName(String typeName) {
        return null;
    }

    @Override
    public void downloadTemplate(String templateCode, HttpServletResponse response) {
        SysFile sysFile = sysFileService.getOne();
        if (sysFile == null) {
            FILE_NOT_EXISTS.throwException();
        }
        String fileName = sysFile.getFileName();
        String fileUrl = sysFile.getFileUrl();
        Resource resource = resourceLoader.getResource(fileUrl);
        if (!resource.exists()) {
            FILE_NOT_EXISTS.throwException();
        }
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        try {
            PrintWriter writer = response.getWriter();
            writer.write(new String(FileUtils.readBytes(resource.getFile()), StandardCharsets.UTF_8));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SysTempFileInfoVO uploadTemplate(String templateCode) {
        return sysTempFileService.uploadTemplate(templateCode);
    }
}